//
//  NullKiller.m
//  Miiow
//
//  Created by YXY on 2017/3/3.
//  Copyright © 2017年 ruiyun. All rights reserved.
//

#import "NullKiller.h"

@implementation NullKiller
+(NSDictionary *)nullDic:(NSMutableDictionary *)myDic
{
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
    for (int i = 0; i < keyArr.count; i ++)
    {
        if ([keyArr[i] isEqualToString:@"image"]) {
            NSMutableArray * arr = [NSMutableArray new];
           // NSLog(@"%@",[resDic objectForKey:keyArr[i]]);
         //   [myDic removeObjectForKey:keyArr[i]];
//            NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
//            [dic setObject:@"" forKey:@"altText"];
//            
//            [dic setObject:@"" forKey:@"format"];
//            [dic setObject:@"" forKey:@"galleryIndex"];
//            [dic setObject:@"" forKey:@"imageType"];
//            [dic setObject:@"" forKey:@"url"];
//            [dic setObject:@"" forKey:@"width"];
//            [arr addObject:dic];
//            [arr addObject:dic];
//            [myDic setObject:arr forKey:keyArr[i]];
//
            
        }
        id obj = [myDic objectForKey:keyArr[i]];
       
        
        obj = [self changeType:obj];
        
        [resDic setObject:obj forKey:keyArr[i]];
    }
    return resDic;
}

//将NSArray中的Null类型的项目转化成@""
+(NSArray *)nullArr:(NSArray *)myArr
{
    NSMutableArray *resArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < myArr.count; i ++)
    {
        id obj = myArr[i];
        
        obj = [self changeType:obj];
        
        
        [resArr addObject:obj];
    }
    return resArr;
}

//将NSString类型的原路返回
+(NSString *)stringToString:(NSString *)string
{
    
    return string;
}

//将Null类型的项目转化成@""
+(NSString *)nullToString
{
    return @"";
}

//主要方法
//类型识别:将所有的NSNull类型转化成@""
+(id)changeType:(id)myObj
{
    if ([myObj isKindOfClass:[NSDictionary class]])
    {
        return [self nullDic:myObj];
    }
    else if([myObj isKindOfClass:[NSArray class]])
    {
       // NSArray * arr = [NSArray new];
        
        return [self nullArr:myObj];
    }
    else if([myObj isKindOfClass:[NSString class]])
    {
        return [self stringToString:myObj];
    }
    else if([myObj isKindOfClass:[NSNull class]])
    {
        return [self nullToString];
    }
    else
    {
        return myObj;
    }
}
@end
